package com.unittec.zk.provider.auth.service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.unittec.zk.provider.auth.entity.SysApplication;
import com.unittec.zk.provider.auth.mapper.SysApplicationMapper;
import com.unittec.zk.provider.root.ServiceRoot;
import com.unittec.zk.sdk.auth.request.Req101502;

/**
 * @author Jason
 * @description 应用Service
 * @date 2019/11/07
 */
@Service
public class SysApplicationService extends ServiceRoot<SysApplicationMapper, SysApplication> {

    /**
     * 根据条件查询应用详情
     *
     * @param appCode
     * @param appKey
     * @return
     */
    public SysApplication getApplicationByEntity(String appCode, String appKey) {
        LambdaQueryWrapper<SysApplication> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysApplication::getAppCode, appCode)
                .eq(SysApplication::getAppKey, appKey);
        return getMapper().selectOne(queryWrapper);
    }

    public IPage<Map<String, Object>> appListForPage(Req101502 req101502) {
        QueryWrapper<SysApplication> qw = new QueryWrapper<SysApplication>();
        qw.eq("1", "1");
        if (StringUtils.isNotEmpty(req101502.getSelectOne())) {
            qw.and(wrapper -> wrapper.like("app_code", req101502.getSelectOne()).or()
                .like("app_name", req101502.getSelectOne()).or().like("mark", req101502.getSelectOne()).or()
                .like("ip", req101502.getSelectOne()).or().like("tenant_id", req101502.getSelectOne())

            );
        }
        return getMapper().appListForPage(new Page<SysApplication>(req101502.getCurrent(), req101502.getSize()), qw);

    }


    /**
     * @return
     */
    public List<SysApplication> findAll() {
        LambdaQueryWrapper<SysApplication> queryWrapper = new LambdaQueryWrapper<>();
        return this.list(queryWrapper);
    }
}
